var light_status = ""; //环境光状态
var con_model = ""; //控制器模式
var con_status = "";  //控制器状态
var token = localStorage.getItem("key");
var proName = $("#proName").val(sessionStorage.getItem("ProName"));//项目名
//proId
//ProName
var map = {};
map.token = token;
map.projectId = sessionStorage.getItem("proId");//项目ID
map.pageNum = "1";//分页数据
map.pageSize = "20";//每页数据
map.name = $("#key").val();//模糊查询
var request = JSON.stringify(map);

var imgNum = 0//项目图纸数量
var altNum = 0//上位机图片数量

conInfo();
conList();

$(function(){
  showMunu();
  hideImg2();
  showImg2();
  
  function showMunu(){
  $("#condition_1").click(function(){
                          $("#downwindows_1").show();
                          $("#downwindows_2").hide();
                          $("#downwindows_3").hide();
                          });
  $("#condition_2").click(function(){
                          $("#downwindows_1").hide();
                          $("#downwindows_2").show();
                          $("#downwindows_3").hide();
                          });
  $("#condition_3").click(function(){
                          $("#downwindows_1").hide();
                          $("#downwindows_2").hide();
                          $("#downwindows_3").show();
                          });
  $("#conditionList_1").children().click(function(){
                                         //alert($(this).text());
                                         $("#condition_1").text($(this).text());
                                         $("#downwindows_1").hide();
                                         showChoose_1($(this).text());
                                         light_status = $(this).text();
                                         queryControllerListByCondition();
                                         });
  $("#conditionList_2").children().on("click",function(){
                                      //alert($(this).text());
                                      
                                      $("#condition_2").text($(this).text())
                                      $("#downwindows_2").hide();
                                      showChoose_2($(this).text());
                                      con_model = $(this).text();
                                      queryControllerListByCondition();
                                      });
  $("#conditionList_3").children().on("click",function(){
                                      //alert($(this).text());
                                      
                                      $("#condition_3").text($(this).text())
                                      $("#downwindows_3").hide();
                                      showChoose_3($(this).text());
                                      con_status = $(this).text();
                                      queryControllerListByCondition();
                                      });
  }
  
  function showChoose_1(data){
  var arr = ['全部','晚上','傍晚','白天','黎明'];
  var index = -1;
  //清空标签上的属性图片
  $("#conditionList_1").find('p').attr('style','color: ;').find('span').remove();
  
  for(var i = 0;i<arr.length;i++){
  if(arr[i] == data){
  index += i + 1;
  }
  }
  $("#conditionList_1").find('p:eq('+index+')').attr('style','color: #b12f41').append("<span><img class='float_r' src='img/conditionList_icon_s.png' style='margin-top: 16px;width: 13px'></span>");
  }
  function showChoose_2(data){
  var arr = ['全部','本地关机','本地1级光','本地2级光','远程关机','远程1级光','远程2级光','远程3级光','远程自动','远程调频','远程配置闪光起始点','远程配置闪光持续时间','远程调节亮度等级','远程配置光控灵敏度'];
  var index = -1;
  //清空标签上的属性图片
  $("#conditionList_2").find('p').attr('style','').find('span').remove();
  
  for(var i = 0;i<arr.length;i++){
  if(arr[i] == data){
  index += i + 1;
  }
  }
  $("#conditionList_2").find('p:eq('+index+')').attr('style','color: #b12f41').append("<span><img class='float_r' src='img/conditionList_icon_s.png' style='margin-top: 16px;width: 13px'></span>");
  }
  function showChoose_3(data){
  var arr = ['全部','正常','故障'];
  var index = -1;
  //清空标签上的属性图片
  $("#conditionList_3").find('p').attr('style','').find('span').remove();
  
  for(var i = 0;i<arr.length;i++){
  if(arr[i] == data){
  index += i + 1;
  }
  }
  $("#conditionList_3").find('p:eq('+index+')').attr('style','color: #b12f41').append("<span><img class='float_r' src='img/conditionList_icon_s.png' style='margin-top: 16px;width: 13px'></span>");
  }
  function showImg2(){
  $("#showImg2").click(function(){
                       $(".pic_atlas_con").show();
                       $("#hideImg2").show();
                       $("#showImg2").hide();
                       });
  }
  function hideImg2() {
  $("#hideImg2").click(function() {
                       $(".pic_atlas_con").hide();
                       $("#hideImg2").hide();
                       $("#showImg2").show();
                       });
  }
  
  });

/**
 * 控制器信息
 */
function conInfo(){
    $.ajax({
           type:"post",
           url: url_prefix +"projectDetail/projectBaseInfo",
           async:true,
           dataType:"JSON",
           data:{
           "request":request
           },
           success: function(data) {
           console.log(data);
           if(data.code == '200') {
           $("#address").text(data.result.address);
           $("#temperature").text("温度：" + data.result.temperature + " 湿度：" + data.result.humidity);
           //$("#controllerAllNum").text(data.result.controllerAllNum);
           //$("#controllerGoodNum").text(data.result.controllerGoodNum + "/" + data.result.controllerBadNum);
           var arr = data.result.projectImg;
           var atlastImg = data.result.atlastImg
           console.log(atlastImg);
           for(var i = 0;i<arr.length; i++){
           var str = "";
           str = "<div class='mui-slider-item'><img class='banner_img' src='"+data.result.projectImg[i].url+"'></div>"
           $("#projectImg").append(str);
           }
           imgNum = data.result.projectImg.length
           $("#proImgPage").html("1/"+imgNum)
           mui('#proImgs').slider()
           var gallery = mui('#proImgs');
           gallery.slider({
                          interval: 5000
                          });
           if(atlastImg.length>0){
           $(".pic_con").show()
           var atl = ""
           for(var i=1;i<=atlastImg.length;i++){
           if(i%4==0){
           atl += '<div class="mui-slider-item"><div class="pic_atlas_con">'
           for(var j=i-3;j<=atlastImg.length;j++){
           atl += '<div class="pic_l_con float_l"><div class="pic_ping_l">'
           atl += '<img class="pic_l" src="'+atlastImg[j-1].url+'">'
           atl += '</div></div>'
           if(j==i){
           break;
           }
           }
           atl += '</div></div>'
           }
           if(i==atlastImg.length){
           atl += '<div class="mui-slider-item"><div class="pic_atlas_con">'
           for(var j=i-i%4+1;j<=atlastImg.length;j++){
           atl += '<div class="pic_l_con float_l"><div class="pic_ping_l">'
           atl += '<img class="pic_l" src="'+atlastImg[j-1].url+'">'
           atl += '</div></div>'
           if(j==i){
           break;
           }
           }
           atl += '</div></div>'
           }
           }
           $("#atlImg").html(atl)
           var num = data.result.atlastImg.length
           if(num%4 == 0){
           altNum = num%4
           }else{
           altNum = num%4 + 1
           }
           $("#atlImgPage").html("1/"+altNum)
           mui('#atlImgs').slider()
           var gallery = mui('#atlImgs');
           gallery.slider({
                          interval: 5000
                          });
           }
           }else if(data.code == '300'){
           mui.toast("账号在其他设备上登录")
           window.location.href = "fastLogin.html";
           }
           },
           error: function(XMLHttpRequest, textStatus, errorThrown) {
           
           mui.toast("连接服务器失败,请稍后重试``````");
           }
           });
    
}

/**
 * 控制器列表
 */
function conList(){
    
    $.ajax({
           type:"post",
           url: url_prefix + "home/controllerList",
           async:true,
           dataType:"JSON",
           data:{
           "request":request
           },
           success:function(data){
           console.log(data);
           if(data.code == '200'){
           $("#conList").empty();
           var arr = data.result.list;
           if(arr.length<1){
           mui.toast('暂无数据...')
           return;
           }
           var yes = 0;
           var no = 0;
           for(var i =0;i<arr.length;i++){
           if(arr[i].controllerState == '正常' || arr[i].controllerState == '未连接' ){
           yes++;
           }else if(arr[i].controllerState == '故障'){
           no++;
           }
           var str = "";
           str += "<div class='conList' onclick='toDetail("+arr[i].controllerId+");'>"+
           "<P class='float_l conList_nub'>"+arr[i].controllerId+"</P>"+
           "<div class='conList_top float_r'>";
           if(arr[i].lightState == '白天' || arr[i].lightState == '黎明'){
           str += "<img class='conList_state_icon float_l' src='img/state_icon_day@2x.png'>";
           }else{
           str += "<img class='conList_state_icon float_l' src='img/state_icon_night@2x.png'>";
           }
           if(arr[i].voltage != null ){
           str += "<p class='conList_voltage float_l'>"+arr[i].voltage+"v</p>";
           }else{
           str += "<p class='conList_voltage float_l'>0v</p>";
           }
           str += "<div class='clear'></div>"+
           "</div>"+
           "<div class='clear'></div>"+
           "<p class='float_l conList_det'>"+arr[i].controllerMode+"</p>"+
           "<p class='float_r conList_state'>"+arr[i].controllerState+"</p>"+
           "<div class='clear'></div>"+
           "</div>";
           $("#conList").append(str);
           }
           $("#controllerAllNum").text(arr.length);
           $("#controllerGoodNum").text(yes+"/"+no);
           }else if(data.code == '300'){
           mui.toast("账号在其他设备上登录")
           window.location.href = "fastLogin.html";
           }
           },
           error: function(XMLHttpRequest, textStatus, errorThrown) {
           
           mui.toast("连接服务器失败,请稍后重试``````");
           }
           })
}
/*
 * 模糊查询
 */
function queryControllerList(){
    var map = {};
    map.token = token;
    map.projectId = sessionStorage.getItem("proId");
    map.pageNum = "1";
    map.pageSize = "20";
    map.name = $("#key").val();
    if(map.name==""){
        conList()
        return
    }
    var request = JSON.stringify(map);
    $.ajax({
           url: url_prefix + "home/queryControllerList",
           type:'POST',
           dataType:'JSON',
           data:{
           "request":request
           },
           success:function(data){
           if(data.code == '200'){
           $("#conList").empty();
           //总条数
           data.result.total;
           //每页条数
           data.result.pageSize;
           //第几页
           data.result.pageNum;
           //结果集
           var arr = data.result.list;
           if(arr.length<1){
           mui.toast('暂无数据...')
           return;
           }
           
           var yes = 0;
           var no = 0;
           for(var i =0;i<arr.length;i++){
           if(arr[i].controllerState == '正常' || arr[i].controllerState == '未连接' ){
           yes++;
           }else if(arr[i].controllerState == '故障'){
           no++;
           }
           var str = "";
           str += "<div class='conList' onclick='toDetail("+arr[i].controllerId+");'>"+
           "<P class='float_l conList_nub'>"+arr[i].controllerId+"</P>"+
           "<div class='conList_top float_r'>";
           if(arr[i].lightState == '白天' || arr[i].lightState == '黎明'){
           str += "<img class='conList_state_icon float_l' src='img/state_icon_day@2x.png'>";
           }else{
           str += "<img class='conList_state_icon float_l' src='img/state_icon_night@2x.png'>";
           }
           if(arr[i].voltage != null ){
           str += "<p class='conList_voltage float_l'>"+arr[i].voltage+"v</p>";
           }else{
           str += "<p class='conList_voltage float_l'>0v</p>";
           }
           str += "<div class='clear'></div>"+
           "</div>"+
           "<div class='clear'></div>"+
           "<p class='float_l conList_det'>"+arr[i].controllerMode+"</p>"+
           "<p class='float_r conList_state'>"+arr[i].controllerState+"</p>"+
           "<div class='clear'></div>"+
           "</div>";
           $("#conList").append(str);
           }
           $("#controllerAllNum").text(arr.length);
           $("#controllerGoodNum").text(yes+"/"+no);
           }else if(data.code == '300'){
           mui.toast("账号在其他设备上登录")
           window.location.href = "fastLogin.html";
           }
           },
           error: function(XMLHttpRequest, textStatus, errorThrown) {
           
           mui.toast("连接服务器失败,请稍后重试``````");
           }
           });
}
/**
 * 列表赛选
 */
function queryControllerListByCondition(){
    var lightState = light_status;
    var controllerMode = con_model;
    var controllerState = con_status;
    var map = {};
    map.token = token;
    map.projectId = sessionStorage.getItem("proId");
    map.pageNum = "1";
    map.pageSize = "20";
    map.name = $("#key").val();
    map.lightState = lightState;
    map.controllerMode = controllerMode;
    map.controllerState = controllerState;
    var request = JSON.stringify(map);
    $.ajax({
           url: url_prefix + "home/queryControllerListByCondition",
           type:'POST',
           dataType:'JSON',
           data:{
           "request":request
           },
           success:function(data){
           console.log(data);
           if(data.code == '200'){
           $("#conList").empty();
           //总条数
           data.result.total;
           //每页条数
           data.result.pageSize;
           //第几页
           data.result.pageNum;
           //结果集
           var arr = data.result.list;
           if(arr.length<1){
           mui.toast('暂无数据...')
           return;
           }
           var yes = 0;
           var no = 0;
           for(var i =0;i<arr.length;i++){
           if(arr[i].controllerState == '正常' || arr[i].controllerState == '未连接' ){
           yes++;
           }else if(arr[i].controllerState == '故障'){
           no++;
           }
           var str = "";
           str += "<div class='conList' onclick='toDetail("+arr[i].controllerId+");'>"+
           "<P class='float_l conList_nub'>"+arr[i].controllerId+"</P>"+
           "<div class='conList_top float_r'>";
           if(arr[i].lightState == '白天' || arr[i].lightState == '黎明'){
           str += "<img class='conList_state_icon float_l' src='img/state_icon_day@2x.png'>";
           }else{
           str += "<img class='conList_state_icon float_l' src='img/state_icon_night@2x.png'>";
           }
           if(arr[i].voltage != null ){
           str += "<p class='conList_voltage float_l'>"+arr[i].voltage+"v</p>";
           }else{
           str += "<p class='conList_voltage float_l'>0v</p>";
           }
           str += "<div class='clear'></div>"+
           "</div>"+
           "<div class='clear'></div>"+
           "<p class='float_l conList_det'>"+arr[i].controllerMode+"</p>"+
           "<p class='float_r conList_state'>"+arr[i].controllerState+"</p>"+
           "<div class='clear'></div>"+
           "</div>";
           $("#conList").append(str);
           }
           $("#controllerAllNum").text(arr.length);
           $("#controllerGoodNum").text(yes+"/"+no);
           }else if(data.code == '300'){
           mui.toast("账号在其他设备上登录")
           window.location.href = "fastLogin.html";
           }
           },
           error: function(XMLHttpRequest, textStatus, errorThrown) {
           
           mui.toast("连接服务器失败,请稍后重试``````");
           }
           });
}
/**
 * 跳转控制器详情
 * @param {Object} id
 */
function toDetail(id){
    window.location.href = "conDetail.html";
    sessionStorage.setItem("conId",id);
}


